const express = require('express');
const doctorRouter = express.Router();
const DB = require("./../db/config");
module.exports = doctorRouter;

// 1. 多条件搜索接口
doctorRouter.post("/doctor/search", function (req, res) {
  let { doctorName, doctorJob, sex } = req.body;

  if (doctorName.trim().length === 0 && doctorJob.trim().length === 0 && sex.trim().length === 0) {
    res.json({ code: 40002, msg: '搜索失败,原因，必须要有搜索的条件。' });
    return;
  }
  // 多条件查询 ，做同时满足条件的判断；
  let param = [];
  let sql = `select * from doctorstable where 1=1`;
  if (doctorName.trim().length !== 0) {
    sql = sql + " and dname = ?";
    param.push(doctorName);
  }
  if (doctorJob.trim().length !== 0) {
    sql = sql + " and jid = ?";
    param.push(doctorJob);
  }
  if (sex.trim().length !== 0) {
    sql = sql + " and dsex = ?";
    param.push(sex);

  }
  sql = sql + ";";


  let cb = (err, data) => {
    if (err === null) {
      res.json({ code: 0, msg: '搜索成功', data })
    } else {
      res.json({ code: 40001, msg: '搜索失败' })
    }
  };
  DB.connect(sql, param, cb);

});